#include "anim.h"
#include "ui_anim.h"
#include "config.h"



Anim::Anim (CenteredWidget *parent) : CenteredWidget (parent), ui (new Ui::Anim)
{
    ui->setupUi(this);

    ui->stop->setIcon (QIcon::fromTheme ("process-stop", QIcon (":/images/process-stop.svg")));

    m_duration = 600;

    m_pub = "SolidROC";

    setWindowModality (Qt::ApplicationModal);
    setWindowTitle (tr ("Veuillez patienter..."));
    setWindowFlags (Qt::X11BypassWindowManagerHint);

    // initialisation vue, scène...

    m_view = ui->view;
    m_view->setAlignment (Qt::AlignCenter);

    m_scene = new QGraphicsScene (this);
    m_pixmapItem = m_scene->addPixmap (QPixmap (":/images/loremIpsum.png"));
    QGraphicsBlurEffect* blur = new QGraphicsBlurEffect (this);
    blur->setBlurHints (QGraphicsBlurEffect::PerformanceHint);
    blur->setBlurRadius (3.0);
    m_pixmapItem->setGraphicsEffect (blur);

    m_item = m_scene->addText ("A");
    m_item->setDefaultTextColor (Qt::black);
    m_item->setGraphicsEffect (new QGraphicsDropShadowEffect (this));
    QFont font;
    font.setBold (true);
    font.setFamily ("Serif");
    m_item->setFont (font);

    m_view->setScene (m_scene);
    m_scene->setSceneRect (m_pixmapItem->boundingRect ());
    m_item->setTransformOriginPoint (m_item->boundingRect ().center ());
    m_item->setPos (m_scene->sceneRect ().center ());

    m_view->setOptimizationFlags (QGraphicsView::DontSavePainterState | QGraphicsView::DontAdjustForAntialiasing);
    m_view->setRenderHints (QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
    m_view->setViewportUpdateMode (QGraphicsView::SmartViewportUpdate);

    // signaux & slots
    connect (ui->stop,
             SIGNAL(clicked()),
             this,
             SLOT(onRejected()));

    m_timer = new QTimer (this);

    connect (m_timer,
             SIGNAL(timeout()),
             this,
             SLOT(onTimerTimeout ()));

    ui->progressBar->hide ();
    ui->view->hide ();

    Config config (0);
    m_isSimpleAnimation = config.optionSimpleAnimation ();
}
//-----------
Anim::~Anim()
{
    delete ui;
}
//------------------------------------
void Anim::changeEvent (QEvent *event)
{
    QWidget::changeEvent(event);
    switch (event->type())
    {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;

    default:
        break;
    }
}
//--------------------------------------
void Anim::showEvent(QShowEvent *event)
{
    if (m_isSimpleAnimation) {
        ui->view->hide ();
        ui->progressBar->setRange (0,0);
        ui->progressBar->show ();
    }
    else {
        m_currentChar = 0;
        ui->view->show ();
        ui->progressBar->hide ();
        onTimerTimeout ();
        m_timer->start (m_duration);
    }
    QWidget::showEvent (event);
}
//--------------------------------------
void Anim::hideEvent(QHideEvent *event)
{
    m_timer->stop ();
    ui->progressBar->setRange (0,0);
    QWidget::hideEvent (event);
}
//-------------------------
void Anim::onTimerTimeout()
{
    QApplication::processEvents ();

    m_item->setPlainText (m_pub.at (m_currentChar));
    m_item->adjustSize ();
    m_item->setTransformOriginPoint (m_item->boundingRect ().center ());

    QParallelAnimationGroup* animationGroup1 = new QParallelAnimationGroup;

    QPropertyAnimation* animScale1 = new QPropertyAnimation (m_item, "scale");
    animScale1->setStartValue (5.0);
    animScale1->setKeyValueAt (0.5, 10.0);
    animScale1->setEndValue (8.0);
    animScale1->setEasingCurve(QEasingCurve::Linear);
    animScale1->setDuration (m_duration);
    animationGroup1->addAnimation(animScale1);

    QPropertyAnimation* animOpacity1 = new QPropertyAnimation(m_item, "opacity");
    animOpacity1->setStartValue(0.3);
    animOpacity1->setKeyValueAt (0.5, 1.0);
    animOpacity1->setEndValue(0.0);
    animOpacity1->setEasingCurve(QEasingCurve::Linear);
    animOpacity1->setDuration(m_duration);
    animationGroup1->addAnimation(animOpacity1);

    animationGroup1->start (QAbstractAnimation::DeleteWhenStopped);

    ++m_currentChar;
    if (m_currentChar >= m_pub.length ())
        m_currentChar = 0;
}
//----------------------
void Anim::onRejected ()
{
    emit stopProcess ();
}
